package com.starsoft.quant.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.starsoft.frame.core.orm.PageObject;
import com.starsoft.frame.core.web.WebUtil;
import com.starsoft.quant.bean.QuantModel;
import com.starsoft.quant.bean.QuantModelParam;
import com.starsoft.quant.service.ModelParamsService;
import com.starsoft.quant.service.QuantModelService;

@RestController
@RequestMapping("/quant/manage")
public class ModelManageController {

	@Autowired
	QuantModelService modelService;
	
	@Autowired
	ModelParamsService modelParamService;
	
	@RequestMapping("/modelquery")
	public Map<String, Object> getPageQuery(HttpServletRequest request){
		PageObject po = WebUtil.getPageObject(request);
		return modelService.pageQuery(po);
	}
	
	@RequestMapping("/modeldetail.json")
	public Map<String, Object> getDetail(HttpServletRequest request){
		String modelCode = request.getParameter("code");
		QuantModel quantModel = modelService.get(modelCode);
		List<QuantModelParam> modelParams = modelParamService.getModelParams(modelCode);

		Map<String, Object> mm = new HashMap<>();
		mm.put("quantModel", quantModel);
		mm.put("modelParams", modelParams);
		return mm;
	}
	
	@RequestMapping("/modelsave")
	public Map<String, Object> modelSave(HttpServletRequest request, QuantModel quantModel){
		modelService.save(quantModel);
		return WebUtil.sucesseModel("策略保存成功");
	}
	
	@RequestMapping("/modeldelete")
	public Map<String, Object> modelDelete(HttpServletRequest request, QuantModel quantModel){
		modelService.delete(quantModel);
		return WebUtil.sucesseModel("策略删除成功");
	}

	@RequestMapping("/modelparamsave")
	public Map<String, Object> modelParamSave(HttpServletRequest request, QuantModelParam quantModelParam){
		modelParamService.saveModelParam(quantModelParam);
		return WebUtil.sucesseModel("策略参数保存成功");
	}
	
	@RequestMapping("/modelparamdelete")
	public Map<String, Object> modelParamDelete(HttpServletRequest request, QuantModelParam quantModelParam){
		modelParamService.deleteModelParam(quantModelParam);
		return WebUtil.sucesseModel("");
	}
	
}
